<?php
	require_once('config.php');
	if(isset($_POST['url_list'])){
		$st_urls = array();
		$urls = array();
		$urls = preg_split('/\r\n/',$_POST['url_list']);
		//standardize the url list
		foreach($urls as $url){
			if($url == ''){
				continue;
			}
			$url = trim($url, "'./");
			$tmp = preg_split('/\/\//', $url);
			// add http or when needed
			if($tmp[0] != 'http' || $tmp[0] != 'https'){
				$url = 'http://' .$tmp[count($tmp)-1];
			}
			$parts = explode('.', $tmp[count($tmp)-1]);
			if($parts[0] !== 'www'){ // don't have www
			 	$url = str_replace('//', '//www.', $url);
			}
			if(count($parts) == 1){ // only have on part of url add post fix
				if(isset($_POST['postfix_net']) && $_POST['postfix_net'] == 'on'){
					array_push($st_urls, $url . ".net");
				}
				if(isset($_POST['postfix_com']) && $_POST['postfix_com'] == 'on'){
					array_push($st_urls, $url . ".com");
				}
				if(isset($_POST['postfix_org']) && $_POST['postfix_org'] == 'on'){
					array_push($st_urls, $url . ".org");
				}
			}else{
				array_push($st_urls, $url);
			}
		}

		$message = '';
		// save pages
		if(!isset($import_dir)){
			die("No import dir is set");
		}else{
			$abs_import_path = dirname($_SERVER['SCRIPT_FILENAME']) . "/" . $import_dir;
			foreach($st_urls as $st_url){
				$content = file_get_contents($st_url);
				if($content==false){
					$message .= "Failed to get the content of $st_url";
				}else{
					// convert url to file name
					$file_name = substr($st_url, 7);
					$file_name = str_replace(".", "_", $file_name);
					$file_name = str_replace("/", "_", $file_name);
					$file_name .= ".htm";
					// put the content to file;
					$result = file_put_contents($abs_import_path . "/" . $file_name, $content);
					if($result == false){
						$message = "Failed to save the content of $st_url";
					}else{
						$message .= "Saved the page $st_url successfully <br>";
					}
				}
			}
			$message .= "<br> List of all imported files";
			$handle=opendir($abs_import_path);
			while (($file = readdir($handle))!==false) {
				if(preg_match('/\.htm/', $file, $matches))
					$message .= "<br><a href=\"$import_dir/$file\">$file</a>";
			}

		}
		echo $message;


closedir($handle);
	}else{
		echo "Empty url list or Direct access is not allowed";
	}
?>